package org.example.myleet.p1710;

import java.util.Arrays;

public class Solution {
    public int maximumUnits(int[][] boxTypes, int truckSize) {
        Arrays.sort(boxTypes, (boxType1, boxType2) -> boxType2[1] - boxType1[1]);
        int result = 0, loadSize = 0, i = 0;
        while (loadSize < truckSize && i < boxTypes.length) {
            int[] boxType = boxTypes[i];
            if (boxType[0] <= truckSize - loadSize) {
                result += boxType[0] * boxType[1];
                loadSize += boxType[0];
            } else {
                result += (truckSize - loadSize) * boxType[1];
                loadSize = truckSize;
            }
            ++i;
        }
        return result;
    }
}
